package com.bjpowernode.web.service;

import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.utils.HttpClientUtils;
import com.bjpowernode.web.config.JdRealNameConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class RealNameService {


    @Resource
    private JdRealNameConfig config;

    //实名认证

    public boolean realName(String phone,String idCard,String name){
        boolean result =false;

        Map<String,String> params =new HashMap<>();
        params.put("cardNo",idCard);
        params.put("realName",name);
        params.put("appkey",config.getAppkey());
        try {
 /*{
    "code": "10000",
    "charge": false,
    "remain": 1305,
    "msg": "查询成功",
    "result": {
        "error_code": 0,
        "reason": "成功",
        "result": {
            "realname": "乐天磊",
            "idcard": "350721197702134399",
            "isok": true
        }
    }
}*/
            //返回json格式字符串的应答结果
            //String json=HttpClientUtils.doGet(config.getUrl(),params);
            String json = "{\n" +
                    "    \"code\": \"10000\",\n" +
                    "    \"charge\": false,\n" +
                    "    \"remain\": 1305,\n" +
                    "    \"msg\": \"查询成功\",\n" +
                    "    \"result\": {\n" +
                    "        \"error_code\": 0,\n" +
                    "        \"reason\": \"成功\",\n" +
                    "        \"result\": {\n" +
                    "            \"realname\": \"乐天磊\",\n" +
                    "            \"idcard\": \"350721197702134399\",\n" +
                    "            \"isok\": true\n" +
                    "        }\n" +
                    "    }\n" +
                    "}";
            if (StringUtils.isNotEmpty(json)){
                //使用fastjson解析JSON格式的字符串
                JSONObject jsonObject =JSONObject.parseObject(json);
                if ("10000".equals(jsonObject.getString("code"))){
                    JSONObject resultJsonObj =jsonObject.getJSONObject("result");
                    if (resultJsonObj!=null){
                        JSONObject innerResult = resultJsonObj.getJSONObject("result");
                        result =innerResult.getBoolean("isok");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
